package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.Goods;
import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.Types;
import cn.duli.xianer.service.TypesService;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/types")
public class TypesController {

    @Autowired
    private TypesService typesService;

    /**
     * 新增商品类别
     * @param types
     */
    @RequestMapping("/save")
    @RequiresRoles("admin")
    public MyResult save(Types types) {
        boolean bool = typesService.checkByTname(types.gettName());
        if (bool) {
            typesService.save(types);
            return new MyResult(200, "新增商品类别成功",null,null,null,null);
        } else {
            return new MyResult(401, "此类别已存在",null,null,null,null);
        }
    }


    /**
     * 修改商品类别
     * @param types
     */
    @RequestMapping("/update")
    @RequiresRoles("admin")
    public MyResult update(Types types) {
        boolean bool = typesService.checkByTname(types.gettName());
        if (bool) {
            typesService.update(types);
            return new MyResult(200, "修改商品类别成功",null,null,null,null);
        } else {
            return new MyResult(401, "此类别已存在",null,null,null,null);
        }
    }

    /**
     * 根据id删除商品类别（t_status=0）
     * @param tId
     */
    @RequestMapping("/deleteById/{tId}")
    @RequiresRoles("admin")
    public MyResult deleteById(@PathVariable("tId") Integer tId) {
        typesService.deleteById(tId);
        return new MyResult(200, "根据id删除商品类别成功",null,null,null,null);
    }

    /**
     * 根据id查找此商品类别详情
     * @param tId
     * @return
     */
    @GetMapping("/findById/{tId}")
    public MyResult findById(@PathVariable("tId") Integer tId){
        Types types = typesService.findById(tId);
        return new MyResult(200, "根据id查找此商品类别详情成功",null,null,types,null);
    }

    /**
     * 查询所有一级商品类别
     * @return
     */
    @GetMapping("/findOneLevels")
    public MyResult findOneLevels(){
        List<Types> typesList = typesService.findOneLevels();
        return new MyResult(200, "根据id查找此商品类别详情成功",null,typesList,null,null);
    }


    /**
     * 根据父类商品类别id查询该父类下的子类二级商品类别
     * @param tParentId
     * @return
     */
    @GetMapping("/findTwoLevels/{tParentId}")
    public MyResult findTwoLevels(@PathVariable("tParentId") Integer tParentId){
        List<Types> typesList = typesService.findTwoLevels(tParentId);
        return new MyResult(200, "根据父类商品类别id查询该父类下的子类二级商品类别成功",null,typesList,null,null);
    }

    /**
     * 根据id取消删除的商品类别(回收站回收某个商品类别)（t_status=1）
     * @param tId
     */
    @RequestMapping("/cancelDeleteById/{tId}")
    @RequiresRoles("admin")
    public MyResult cancelDeleteById(@PathVariable("tId") Integer tId) {
        typesService.cancelDeleteById(tId);
        return new MyResult(200, "根据id取消删除的商品类别成功",null,null,null,null);
    }


}
